package osgi.common.services.job;

import org.quartz.Job;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.simpl.PropertySettingJobFactory;
import org.quartz.spi.TriggerFiredBundle;

import osgi.common.services.job.client.ICommonJob;

public class CustomOSGIJobFactory extends PropertySettingJobFactory {

	
	
	protected IQuartzService quartzService;
	
	
	
	public CustomOSGIJobFactory(IQuartzService quartzService) {
		super();
		this.quartzService = quartzService;
	}



	@Override
	public Job newJob(TriggerFiredBundle firedBundle, Scheduler scheduler) throws SchedulerException {
		
        Job returnedJob = null;
        
        if(firedBundle.getJobDetail().getJobDataMap().containsKey(ICommonJob.OSGI_JOB_NAME)){
        	
            returnedJob = this.quartzService.getRegisteredJob(
            		                                 firedBundle.
            		                                    getJobDetail().
            		                                      getJobDataMap().
            		                                        getString(ICommonJob.OSGI_JOB_NAME)
            		                               );                          
            
        }else{
        	
        	returnedJob = super.newJob(firedBundle, scheduler);
        	
        }
        
		return returnedJob;
		
	}
	
	

}
